// Import System requirements
import Vue from 'vue'
import VueRouter from 'vue-router'
import VueResource from 'vue-resource'
import { sync } from 'vuex-router-sync'
import routes from './routes'
import store from './store'
import BootstrapVue from 'bootstrap-vue'
import RestApi from './common/restapi'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//import 'element-ui/lib/theme-default/index.css'


// Import Helpers for filters
import { domain, count, prettyDate, pluralize } from './filters'

// Import Views - Top level
import AppView from './components/App.vue'

// Import Install and register helper items
Vue.filter('count', count)
Vue.filter('domain', domain)
Vue.filter('prettyDate', prettyDate)
Vue.filter('pluralize', pluralize)

Vue.use(VueRouter)
Vue.use(BootstrapVue)
Vue.use(VueResource)
Vue.prototype.RestApi = RestApi
Vue.use(ElementUI)




// Routing logic
var router = new VueRouter({
  routes: routes,
  mode: 'history',
  scrollBehavior: function (to, from, savedPosition) {
    return savedPosition || { x: 0, y: 0 }
  }
})

// Check local storage to handle refreshes
if (window.sessionStorage) {
  var expire = window.sessionStorage.getItem('xLoginUser') || 'null'

  var localUserString = window.sessionStorage.getItem('xLoginUser') || 'null'
  var localUser = JSON.parse(localUserString)

  if (localUser && store.state.user !== localUser) {
    store.commit('SET_USER', localUser)
  }

  var localGraph = window.sessionStorage.getItem('xGraph') || 'null'
  if (localGraph) {
    store.commit('SET_GRAPH', JSON.parse(localGraph))
  }

  var localhandle = window.sessionStorage.getItem('xhandle') || 'null'
  if (localhandle) {
    store.commit('SET_HANDLE', JSON.parse(localhandle))
  }

  var localsubmit = window.sessionStorage.getItem('xsubmit') || 'null'
  if (localsubmit) {
    store.commit('SET_SUBMIT', JSON.parse(localsubmit))
  }

  var sysconfig = window.sessionStorage.getItem('sysconfig') || 'null'
  if (sysconfig) {
    store.commit('SET_SYSCONFIG', JSON.parse(sysconfig))
  }

  var departs = window.sessionStorage.getItem('departs') || 'null'
  if (sysconfig && !store.state.departments) {
    store.commit('SET_DEPARTS', JSON.parse(departs))
  }

  var userDepart = window.sessionStorage.getItem('xdepart') || 'null'
  if (userDepart) {
    store.commit("SET_DEPART",JSON.parse(userDepart));
  }
}

// Vue.directive('focus', {
//   // When the bound element is inserted into the DOM...
//   inserted: function (el) {
//     // Focus the element
//     el.focus()
//   }
// })

// Some middleware to help us ensure the user is authenticated.
router.beforeEach((to, from, next) => {
  console.log("start to:"+to.path+"@"+new Date())
  if(store.state.user){
    Vue.http.headers.common['token'] = store.state.user.token
    Vue.http.headers.common['user'] = store.state.user.id.toString()
    Vue.http.headers.common['role'] = store.state.user.role.id.toString()
  }

  if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
    if (store.state.user) {  // 通过vuex state获取当前的token是否存在
      if((store.state.user.role.roles >= to.meta.roleRequired)){
        next();
        console.log("next to:"+to.path+"@"+new Date())
      }
      else{
        next({
          path: '/users',
          query: { redirect: to.fullPath }  // 将跳转的路由path作为参数，登录成功后跳转到该路由
        })
      }
    }
    else {
      next({
        path: '/login',
        query: { redirect: to.fullPath }  // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  }
  else {
    next();
  }
})

sync(store, router)

// Start out app!
// eslint-disable-next-line no-new
new Vue({
  el: '#root',
  router: router,
  store: store,
  render: h => h(AppView)
})


